package cn.passlink.bletoothchat.SqlTables;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.passlink.bletoothchat.Entity.singleGroup;

public class singleGroupTableManager {
    private SQLiteDatabase database;

    public singleGroupTableManager(SQLiteDatabase database,String tablenaem) {
        this.database = database;
    }

    public void addMember(singleGroup member) {
        ContentValues values = new ContentValues();
        values.put("member_name", member.getMember_name());
        values.put("member_phone", member.getMember_phone());
        database.insert("singleGroup", null, values);
    }

    public void deleteMember(int memberId) {
        String whereClause = "id" + " = ?";
        String[] whereArgs = {String.valueOf(memberId)};
        database.delete("singleGroup", whereClause, whereArgs);
    }

    public void updateMember(singleGroup member) {
        ContentValues values = new ContentValues();
        values.put("member_name", member.getMember_name());
        values.put("member_phone", member.getMember_phone());

        String whereClause = "member_name" + " = ?";
        String[] whereArgs = {String.valueOf(member.getMember_name())};
        database.update("singleGroup", values, whereClause, whereArgs);
    }

    public singleGroup getMemberById(int memberId) {
        String[] projection = {"id", "member_name", "member_phone"};
        String selection = "id" + " = ?";
        String[] selectionArgs = {String.valueOf(memberId)};

        Cursor cursor = database.query("singleGroup", projection, selection, selectionArgs, null, null, null);
        if (cursor.moveToFirst()) {
            singleGroup member = new singleGroup(cursor.getString(cursor.getColumnIndexOrThrow("member_name")),cursor.getString(cursor.getColumnIndexOrThrow("member_phone")));
            cursor.close();
            return member;
        }

        cursor.close();
        return null;
    }
}